﻿var $root = $('html, body');
$('.enroll , .enroll-lg').click(function () {
    var top = $($(this).attr('href')).offset().top;
    $root.animate({
        scrollTop: top
    }, 500);
    return false;
});
//报名事件
$(function () {
    GetRows();
    //select
    $("[verify='select']").change(function () {
        var tips = $(this).attr("date-tips");
        var sel = $(this).find("option:selected").text();

    })
    //submit
    $("#submit").click(function () {
        var varify = Verify();
        if (varify) {
            $.ajax({
                url: $(this).attr('a_url'),
                type: 'post',
                data:{
                	'apply_id':$("input[name='apply_id']").val(),
                	'recruit_id':$("input[name='recruit_id']").val(),
                	'apply_avatar':$("input[name='apply_avatar']").val(),
                	'recruit_post_id':$("select[name='recruit_post_id']").val(),
                	'is_obeyed':$("select[name='is_obeyed']").val(),
                	'apply_name':$("input[name='apply_name']").val(),
                	'apply_sex':$("select[name='apply_sex']").val(),
                	'apply_birthday':$("input[name='apply_birthday']").val(),
                	'apply_idcard':$("input[name='apply_idcard']").val(),
                	'apply_nation':$("input[name='apply_nation']").val(),
                	'apply_identity':$("select[name='apply_identity']").val(),
                	'apply_marriage':$("select[name='apply_marriage']").val(),
                	'apply_mobile':$("input[name='apply_mobile']").val(),
                	'apply_school':$("input[name='apply_school']").val(),
                	'apply_specialty':$("input[name='apply_specialty']").val(),
                	'apply_diploma':$("select[name='apply_diploma']").val(),
                	'is_fulltime':$("select[name='is_fulltime']").val(),
                	'apply_degree':$("select[name='apply_degree']").val(),
                	'apply_paddress':$("input[name='apply_paddress']").val(),
                	'apply_address':$("input[name='apply_address']").val(),
                	'apply_career1':$("input[name='apply_career1']").val(),
                	'apply_career2':$("input[name='apply_career2']").val(),
                	'apply_career3':$("input[name='apply_career3']").val(),
                	'apply_certificate':$("input[name='apply_certificate']").val(),
                	'apply_prize':$("input[name='apply_prize']").val()
                	
                },
                success: function (data) {
                    if (data == '0') {
                        alert('报名失败!');
                    } else if (data == 'IsExist') {
                        alert('已经报名,不要重复填报!');
                        window.location.reload();
                    } else if (data == 'Error') {
                        alert('出错，请重新填报!');
                        window.location.reload();
                    }else{
                        alert('恭喜您,报名成功!\r\n请留意审核结果!');
                        window.location.reload();
                    }  
                }
            });
        }
    });
});

function Verify() {
    var ex = true;
    $("[verify]").each(function () {
        var ve = $(this).attr("verify");
        var val = $(this).val();
        var tips = $(this).attr("date-tips");
        var date_YM = /^[1,2][0,9][0-9][0-9][.][0,1][0-9]$/;
        var phone = /^(\d{11})$/;
        var num = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
        var sel = $(this).find("option:selected").text();
        if (ve == 'text') {
            if (val == '') {
                alert(tips);
                $(this).focus();
                ex = false;
                return false;
            }
        }
        if (ve == 'date_YM') {
            if (val == '') {
                alert(tips);
                $(this).focus();
                ex = false;
                return false;
            }
            else if (!date_YM.test(val)) {
                alert("请按格式填写日期，例：【2017.01】")
                $(this).focus();
                ex = false;
                return false;
            }
        }
        if (ve == 'number') {
            var checkFlag = new clsIDCard(val);
            if (!checkFlag.IsValid()) {
                alert(tips);
                //$(this).focus();
                ex = false;
                return false;
            }
        }
        if (ve == 'tel') {
            if (val == '') {
                alert(tips);
                $(this).focus();
                ex = false;
                return false;
            }
            else if (!phone.test(val)) {
                alert("请输入有效的手机号码！")
                $(this).focus();
                ex = false;
                return false;
            }
        }

        if (ve == 'select') {
            if (sel == '请选择' || sel == "省份" || sel == "地级市" || sel == "市、县级市") {
                alert(tips);
                $(this).focus();
                ex = false;
                return false;
            }
        }
        if (ve == 'check') {
        	if (val=='') {
        		alert(tips);
        		ex = false;
        		return false;
        	}
        }

    })

    if (ex==true&&window.pageVerify)
        ex = pageVerify();

    return ex;
}

function GetRows() {
    var rows = "";
    var cols = "";
    for (var i = 0; i <= 40; i++) {
        if ($("#info" + i).length > 0) {
            rows += "info" + i + ",";
            cols += $("#info" + i).attr("date-col") + ",";
        }
    }
    if($("#s_province").length>0){
        rows += $("#s_province").attr("name")+",";
        cols += $("#s_province").attr("date-col") + ",";
    }

    if($("#s_city").length>0){
        rows += $("#s_city").attr("name") + ",";
        cols += $("#s_city").attr("date-col") + ",";
    }

    if($("#s_county").length>0){
        rows += $("#s_county").attr("name") + ",";
        cols += $("#s_county").attr("date-col") + ",";
    }
    if ($("#PPhotoUrl").length > 0) {
        rows += $("#PPhotoUrl").attr("name") + ",";
        cols += $("#PPhotoUrl").attr("date-col") + ",";
    }

    $("#rows").val(rows.substr(0, rows.length - 1));
    $("#cols").val(cols.substr(0, cols.length - 1));
}

//数据分析图
$(document).ready(function () {
    $(".z-click").click(function () {
        var datacolname = $(this).prev().children().attr("date-col");
        var datacol = $(this).prev().children().attr("id");
        var dataparent = "";
        var dataparentval = "";

        if (typeof ($(this).attr("parent")) != "undefined") {
            dataparent = $(this).attr("parent");
            dataparentval = $("#" + dataparent).val();
        }

        var obj = $("#" + datacol + "-count");

        if (obj.length == 0) {
            $(this).after("<div class=\"z-show\" style=\"display:none;\"><div id=\"" + datacol + "-count\" style=\"width: 100%;height:400px;margin:0 auto;\"></div></div>");
        }
        $(this).parent().find(".z-show").toggle();
        $(this).parent().css("z-index", "9999");
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById(datacol + '-count'));

        myChart.setOption({
            title: {
                text: '',
                textStyle: {
                    fontSize: 18,
                    fontWeight: 'bolder',
                    color: '#333'          // 主标题文字颜色
                }
            },
            xAxis: {
                data: [],
                axisTick: {
                    alignWithLabel: true
                }
            },
            yAxis: {},
            grid: {
                x: 30,
                y: 100,
                x2: 10,
                y2: 30,
                containLabel: true
            },
            series: [{
                name: '销量',
                type: 'bar',
                data: [],
                barWidth: '60%',
                itemStyle: {
                    normal: {
                        label: { show: true, position: 'top', data: '{data} ' }
                    }
                }
            }]
        });

        if (!$(this).parent().find(".z-show").is(":hidden")) {
            $(".z-show").not($(this).parent().find(".z-show")).hide(); 
            $(".form-group").not($(this).parent()).css("z-index", "0");
            $.ajax({
                url: '/ashx/CountHandler.ashx',
                data: { colname: datacolname, colvalue: datacol, parent: dataparent, parentval: dataparentval },
                type: 'POST', //GET
                async: true,    //或false,是否异步
                success: function (str) {
                    var data = eval('(' + str + ')');
                    //if (data.categories)
                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption({
                        title: {
                            text: data.title + "分析"
                        },
                        xAxis: {
                            data: data.categories,
                            axisLabel: {
                                formatter: function (val) {
                                    if (typeof (val) != "undefined")
                                        return val.split("").join("\n");
                                    else
                                        return "无";
                                }
                            },
                        },
                        series: [{
                            // 根据名字对应到相应的系列
                            name: data.title,
                            data: data.data
                        }]
                    });

                    window.onresize = myChart.resize;
                    $(".z-show").on("click", function () {
                        $(this).hide();
                    })

                }
            });
        }
    })

})